package cn.net.autocode.platform.customDict.dict;

import cn.net.autocode.core.bean.ResultT;
import cn.net.autocode.dbManager.Dao;
import cn.net.autocode.platform.customDict.CustomDict;
import cn.net.autocode.platform.customDict.CustomDictAnnotation;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


@CustomDictAnnotation("fkField")
@Component("autocode_platform_fkField")
public class FkField implements CustomDict {
    private final Dao dictDao;

    public FkField(@Qualifier("dictDao") Dao dictDao) {
        this.dictDao = dictDao;
    }

    @Override
    public ResultT query(JSONObject param) {
        JSONArray array = dictDao.queryJSONArray("select t1.name value,t1.name label,t1.* from ac_table_fields t1,ac_table t2 where t1.table_id=t2.id and t2.table_name=? " +
                " and not exists(select 1 from ac_table_ref t3 where t3.table_id=? and t3.fk_table=? and t3.fk_field=t1.name)" +
                " order by t1.name", param.getString("fkTable"),param.getString("tableId"),param.getString("fkTable"));

        return ResultT.success(array);
    }
}
